package com.jic.product.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.CategoryPropertyName;
import com.jic.product.entity.CategoryPropertyValue;
import com.jic.product.entity.ProductPropertyNameRelation;
import com.jic.product.entity.ProductPropertyValueRelation;
import com.jic.product.manage.request.*;
import com.jic.product.mapper.CategoryPropertyNameMapper;
import com.jic.product.mapper.CategoryPropertyValueMapper;
import com.jic.product.mapper.ProductPropertyNameRelationMapper;
import com.jic.product.manage.response.CategoryPropertyNameResponse;
import com.jic.product.manage.response.CategoryPropertyValueResponse;
import com.jic.product.service.CategoryPropertyNameService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类目属性名
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-10 07:03:01
 */
@Service
public class CategoryPropertyNameServiceImpl implements CategoryPropertyNameService {

    @Autowired
    private CategoryPropertyNameMapper categoryPropertyNameMapper;

    @Autowired
    private CategoryPropertyValueMapper categoryPropertyValueMapper;

    @Autowired
    private ProductPropertyNameRelationMapper productPropertyNameRelationMapper;

    @Override
    public long insert(CategoryPropertyName categoryPropertyName) {
        long flag = categoryPropertyNameMapper.insert(categoryPropertyName);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return categoryPropertyNameMapper.delete(id);
    }

    @Override
    public long update(CategoryPropertyName categoryPropertyName) {
        return categoryPropertyNameMapper.update(categoryPropertyName);
    }

    @Override
    public CategoryPropertyName queryByPrimaryKey(Long id) {
        return categoryPropertyNameMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<CategoryPropertyName> query(CategoryPropertyName categoryPropertyName) {
        return categoryPropertyNameMapper.query(categoryPropertyName);
    }

    @Override
    public PageResult<CategoryPropertyName> queryPage(CategoryPropertyName categoryPropertyName, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<CategoryPropertyName> list = categoryPropertyNameMapper.queryPage(startIndex, pageQuery.getLimit(), categoryPropertyName);
        long count = categoryPropertyNameMapper.queryPageCount(categoryPropertyName);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<List<CategoryPropertyNameResponse>> getCategoryPropertyName(CategoryPropertyNameRequest categoryPropertyNameRequest) {
        //查询所有属名
        CategoryPropertyName categoryPropertyName = BeanUtil.copy(categoryPropertyNameRequest, CategoryPropertyName.class);
        List<CategoryPropertyNameResponse> categoryPropertyNameResponseList = categoryPropertyNameMapper.getCategoryPropertyNameList(categoryPropertyName);
        for (CategoryPropertyNameResponse categoryPropertyNameResponse : categoryPropertyNameResponseList) {
            //查询所有属值
            CategoryPropertyValue categoryPropertyValue = new CategoryPropertyValue();
            categoryPropertyValue.setCategoryAttNameId(categoryPropertyNameResponse.getId());
            List<CategoryPropertyValueResponse> categoryPropertyValueResponseList = categoryPropertyValueMapper.getCategoryPropertyValueList(categoryPropertyValue);
            for (CategoryPropertyValueResponse categoryPropertyValueResponse : categoryPropertyValueResponseList) {
                //判断是否是自定义属性名
                if (categoryPropertyValueResponse.getValueType() == 2) {
                    categoryPropertyValueResponse.setIsChecked(3);
                } else {
                    ProductPropertyNameRelation productPropertyNameRelation = new ProductPropertyNameRelation();
                    productPropertyNameRelation.setAttNameId(categoryPropertyNameResponse.getAttrId());
                    productPropertyNameRelation.setProductId(categoryPropertyNameRequest.getProductId());
                    productPropertyNameRelation.setAttValueId(categoryPropertyValueResponse.getValueId());
                    //判断类目属值是否选中
                    long count = productPropertyNameRelationMapper.queryProductPropertyNameRelationCount(productPropertyNameRelation);
                    if (count > 0) {
                        categoryPropertyValueResponse.setIsChecked(1);
                    } else {
                        categoryPropertyValueResponse.setIsChecked(2);
                    }
                }

            }
            categoryPropertyNameResponse.setCategoryPropertyValueResponseList(categoryPropertyValueResponseList);
        }
        return RestResult.success(categoryPropertyNameResponseList);
    }

    @Override
    public RestResult<Boolean> saveCategoryPropertyName(CategoryPropertyNameListRequest categoryPropertyNameListRequest) {

        for (CategoryPropertyNameRequest productPropertyRequest : categoryPropertyNameListRequest.getCategoryPropertyNameRequestList()) {
            //保存属性名
            CategoryPropertyName categoryPropertyName = new CategoryPropertyName();
            categoryPropertyName.setAttNameId(productPropertyRequest.getAttNameId());
            categoryPropertyName.setCategoryId(categoryPropertyNameListRequest.getCategoryId());
            categoryPropertyName.setCreateTime(new Date());
            categoryPropertyNameMapper.insert(categoryPropertyName);
            for (CategoryPropertyValueRequest categoryPropertyValueRequest : productPropertyRequest.getCategoryPropertyValueRequestList()) {
                //保存属性值
                CategoryPropertyValue categoryPropertyValue = new CategoryPropertyValue();
                categoryPropertyValue.setCategoryAttNameId(categoryPropertyName.getId());
                categoryPropertyValue.setAttValueId(categoryPropertyValueRequest.getAttValueId());
                categoryPropertyValue.setCreateTime(new Date());
                categoryPropertyValueMapper.insert(categoryPropertyValue);
            }

        }
        return RestResult.success(true);
    }

}
